package com.macro.mall.service.impl;

import cn.hutool.core.date.DateTime;
import com.macro.mall.dto.BmsSightseeingBusParam;
import com.macro.mall.mapper.BmsSightseeingBusMapper;
import com.macro.mall.mapper.BmsSightseeingBusPicMapper;
import com.macro.mall.mapper.BmsSightseeingBusPointMapper;
import com.macro.mall.model.BmsSightseeingBus;
import com.macro.mall.model.BmsSightseeingBusPic;
import com.macro.mall.service.BmsSightseeingBusService;
import com.macro.mall.utils.UserUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;


/**
 * 商品属性管理Service实现类
 * Created by macro on 2018/4/26.
 */
@Service
public class BmsSightseeingBusServiceImpl implements BmsSightseeingBusService {
    @Autowired
    private BmsSightseeingBusMapper bmsSightseeingBusMapper;
    @Autowired
    private UserUtils userUtils;

    @Autowired
    private BmsSightseeingBusPicMapper bmsSightseeingBusPicMapper;
    @Autowired
    private  BmsSightseeingBusPointMapper bmsSightseeingBusPointMapper;


    /**
     * 分页查询观光车
     * @return
     */
    @Transactional
    @Override
    public List<BmsSightseeingBus> getList(BmsSightseeingBusParam bmsSightseeingBusParam) {
        BmsSightseeingBus bmsSightseeingBus = new BmsSightseeingBus();
        BeanUtils.copyProperties(bmsSightseeingBusParam, bmsSightseeingBus);
        List<BmsSightseeingBus> list = bmsSightseeingBusMapper.selectAll(bmsSightseeingBus);
        return list;
    }

    /**
     * 新增观光车
     * @param
     * @return
     */
    @Transactional
    @Override
    public int create(BmsSightseeingBusParam bmsSightseeingBusParam) {
        int count = 0;
        //获取当前登录用户id
        Long userId = userUtils.getUserId();
        BmsSightseeingBus bmsSightseeingBus = new BmsSightseeingBus();
        BeanUtils.copyProperties(bmsSightseeingBusParam, bmsSightseeingBus);
        bmsSightseeingBus.setCreateTime(new DateTime());
        bmsSightseeingBus.setUpdateTime(new DateTime());
        bmsSightseeingBus.setCreateUser(userId);
        bmsSightseeingBus.setUpdateUser(userId);
        if (bmsSightseeingBusMapper.insertSelective(bmsSightseeingBus) > 0){
            //是封面图
            String coverPic = bmsSightseeingBusParam.getCoverPic();
            //不是封面图
            List<String> picList = bmsSightseeingBusParam.getPicList();
            BmsSightseeingBusPic bmsSightseeingBusPic;
            if (StringUtils.isNotBlank(coverPic)){
                bmsSightseeingBusPic = new BmsSightseeingBusPic();
                bmsSightseeingBusPic.setPic(coverPic);
                bmsSightseeingBusPic.setSightseeingBusId(bmsSightseeingBus.getId());
                bmsSightseeingBusPic.setIsCover(1);
                if (bmsSightseeingBusPicMapper.insert(bmsSightseeingBusPic) < 0){
                    return -1;
                }
            }
            if (CollectionUtils.isNotEmpty(picList)) {
                for (String pic : picList) {
                    bmsSightseeingBusPic = new BmsSightseeingBusPic();
                    bmsSightseeingBusPic.setPic(pic);
                    bmsSightseeingBusPic.setSightseeingBusId(bmsSightseeingBus.getId());
                    bmsSightseeingBusPic.setIsCover(0);
                    if (bmsSightseeingBusPicMapper.insert(bmsSightseeingBusPic) <0){
                        return -1;
                    }
                }
            }
            count = 1;
            return count;
        }
        return count;
    }

    /**
     * 更新观光车
     * @param
     * @return
     */
    @Transactional
    @Override
    public int update(BmsSightseeingBusParam bmsSightseeingBusParam) {
        int count = 1;
        //获取当前登录用户id
        Long userId = userUtils.getUserId();
        //根据主表观光车的id，去更新一下主表的信息
        BmsSightseeingBus bmsSightseeingBus = new BmsSightseeingBus();
        bmsSightseeingBus.setId(bmsSightseeingBusParam.getId());
        BeanUtils.copyProperties(bmsSightseeingBusParam, bmsSightseeingBus);
        bmsSightseeingBus.setUpdateTime(new DateTime());
        bmsSightseeingBus.setUpdateUser(userId);
        if (bmsSightseeingBusMapper.updateId(bmsSightseeingBus) > 0) {
            //删除观光车子表的数据，然后新增
            List<Long> ids = new ArrayList<>();
            ids.add(bmsSightseeingBus.getId());
            BmsSightseeingBusPic bmsSightseeingBusPic;

            if (bmsSightseeingBusPicMapper.delete(ids) > 0) {
                //是封面图
                String coverPic = bmsSightseeingBusParam.getCoverPic();
                //不是封面图
                List<String> picList = bmsSightseeingBusParam.getPicList();
                if (StringUtils.isNotBlank(coverPic)) {
                    bmsSightseeingBusPic = new BmsSightseeingBusPic();
                    bmsSightseeingBusPic.setSightseeingBusId(bmsSightseeingBus.getId());
                    bmsSightseeingBusPic.setIsCover(1);
                    bmsSightseeingBusPic.setPic(bmsSightseeingBusParam.getCoverPic());
                    if (bmsSightseeingBusPicMapper.insert(bmsSightseeingBusPic) < 0) {
                        return -1;
                    }
                }
                if (CollectionUtils.isNotEmpty(picList)) {
                    for (String pic : picList) {
                        bmsSightseeingBusPic = new BmsSightseeingBusPic();
                        bmsSightseeingBusPic.setSightseeingBusId(bmsSightseeingBus.getId());
                        bmsSightseeingBusPic.setIsCover(0);
                        bmsSightseeingBusPic.setPic(pic);
                        if (bmsSightseeingBusPicMapper.insert(bmsSightseeingBusPic) < 0){
                            return -1;
                        }
                    }
                }
            }
        } else {
            count = 0;
            return count;
        }
        return count;
    }


    /**
     * 获取单个观光车信息
     * @param id
     * @return
     */
    @Override
    public BmsSightseeingBus getItem(Long id) {
        return bmsSightseeingBusMapper.selectPcDetail(id);
    }

    /**
     * 删除观光车
     * @param ids
     * @return
     */
    @Transactional
    @Override
    public int delete(List<Long> ids) {
        bmsSightseeingBusMapper.updateByNew(ids);
        bmsSightseeingBusPicMapper.delete(ids);
        return 1;
    }

    /**
     * 修改观光车状态
     * @param bmsSightseeingBusParam
     * @return
     */
    @Transactional
    @Override
    public int updateShowStatus(BmsSightseeingBusParam bmsSightseeingBusParam) {
        //获取当前登录用户id
        Long userId = userUtils.getUserId();
        BmsSightseeingBus bmsSightseeingBus = new BmsSightseeingBus();
        BeanUtils.copyProperties(bmsSightseeingBusParam, bmsSightseeingBus);
        bmsSightseeingBus.setId(bmsSightseeingBusParam.getId());
        bmsSightseeingBus.setUpdateTime(new DateTime());
        bmsSightseeingBus.setUpdateUser(userId);
        return bmsSightseeingBusMapper.updateByPrimaryKeySelective(bmsSightseeingBus);
    }



    /**
     * 小程序查询观光车
     * @return
     */
    @Transactional
    @Override
    public List<BmsSightseeingBus> getMpList() {
        List<BmsSightseeingBus> list = bmsSightseeingBusMapper.selectXiaochengxu();
        return list;
    }


    /**
     * 获取单个观光车信息
     */
    @Override
    public List<BmsSightseeingBus> getMpDetail(Long id) {
        return bmsSightseeingBusMapper.selectMpDetail(id);
    }
}
